import { createApp } from 'vue'
import App from './App.vue'
import router from './routers/router-home'
import axiosIns from './util/axios'
import ElementPlus from 'element-plus';
import 'element-plus/dist/index.css'; // 引入Element Plus的CSS样式
import { ElMessage } from 'element-plus';

const app = createApp(App)
app.use(router)
app.use(ElementPlus)
// 定义全局的提示消息函数
app.config.globalProperties.$message =ElMessage

var userId = localStorage.getItem("userId");
if (null == userId || '' == userId || undefined == userId) {
	userId = generateUUID();
	// 保存到localStorage
	localStorage.setItem('userId', userId);
}
console.log("userId:" + userId);
const param = {
	"userId": "tm-" + userId,
	"userName": "tm-" + checkDeviceType(),
	"userVsId": "tm-" + userId,
	"userPhone": "tm-" + userId,
	"userAge": "tm-" + userId,
	"userNick": "tm-" + userId,
}
axiosIns.post("/setUser", param).then(response => {
	// 在main.ts中声明一个全局变量，可以在组件中获取
	// 在应用实例上定义全局数据
	app.config.globalProperties.$globalData = {
		userCount: response.data.count,
	};
	app.mount('#app');
}).catch(error => {
	console.error("访问错误",error);
	app.config.globalProperties.$globalData = {
		userCount: 0,
	};
	app.mount('#app');
});
//const response = await axiosIns.post("/setUserId",param);
//console.log("response-"+JSON.stringify(response));
function checkDeviceType() {
	const screenWidth = window.innerWidth;
	let deviceType = '';
	if (screenWidth < 768) {
		deviceType = '手机';
	} else if (screenWidth >= 768 && screenWidth < 1024) {
		deviceType = '平板';
	} else {
		deviceType = '桌面';
	}
	return deviceType;
}
// 生成UUID函数
function generateUUID() {
	const timestamp = Date.now().toString(36);
	const randomPart = Math.random().toString(36).substring(2, 8);
	return `${timestamp}-${randomPart}`;
}